package io.chenglicun.modules.knowledge.controller;

import io.chenglicun.common.annotation.LogOperation;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.utils.ExcelUtils;
import io.chenglicun.common.utils.Result;
import io.chenglicun.common.validator.AssertUtils;
import io.chenglicun.common.validator.ValidatorUtils;
import io.chenglicun.common.validator.group.AddGroup;
import io.chenglicun.common.validator.group.DefaultGroup;
import io.chenglicun.common.validator.group.UpdateGroup;
import io.chenglicun.modules.knowledge.dto.KnowledgeExtraDTO;
import io.chenglicun.modules.knowledge.dto.UploadRespDTO;
import io.chenglicun.modules.knowledge.excel.KnowledgeExtraExcel;
import io.chenglicun.modules.knowledge.service.KnowledgeExtraService;
import io.chenglicun.modules.knowledge.service.KnowledgeFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
* 知识库文件管理
*
* @author shicong 705740711@qq.com
* @since 1.0 2024-05-20
*/
@RestController
@RequestMapping("knowledge/file")
@Api(tags="知识库文件管理")
public class KnowledgeFileController {
    @Autowired
    private KnowledgeFileService knowledgeFileService;


    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "name", value = "文件名称", paramType = "query", required = false, dataType="String")
    })
//    @RequiresPermissions("knowledge:knowledgefile:page")
    public Result<PageData<UploadRespDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<UploadRespDTO> page = knowledgeFileService.page(params);

        return new Result<PageData<UploadRespDTO>>().ok(page);
    }

    @DeleteMapping
    @ApiOperation("删除文件")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        knowledgeFileService.delete(ids);

        return new Result();
    }


    @PostMapping("upload")
    @ApiOperation(value = "上传文件",httpMethod = "Get",notes = "上传文件。",position = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "上传的文件",defaultValue = "",paramType = "query", dataType="file",required = true)
    })
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {

        knowledgeFileService.upload(file);

        return new Result();
    }


/*    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("knowledge:knowledgeextra:info")
    public Result<KnowledgeExtraDTO> get(@PathVariable("id") Long id){
        KnowledgeExtraDTO data = knowledgeExtraService.get(id);

        return new Result<KnowledgeExtraDTO>().ok(data);
    }*/


/*    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("knowledge:knowledgeextra:save")
    public Result save(@RequestBody KnowledgeExtraDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        knowledgeExtraService.save(dto);

        return new Result();
    }*/

    /*@PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("knowledge:knowledgeextra:update")
    public Result update(@RequestBody KnowledgeExtraDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        knowledgeExtraService.update(dto);

        return new Result();
    }*/

/*    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("knowledge:knowledgeextra:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        knowledgeExtraService.delete(ids);

        return new Result();
    }*/

}